package com.zcx.qshop.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.qshop.entity.Good;

import org.json.JSONArray;

import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/24 0024.
 */
@HttpInlet(Conn.INLET_JSON_INDEX)
public class JsonIndexAsyGet extends QSAsyGet<JsonIndexAsyGet.Info> {

    public String uid, areaid, page;

    public JsonIndexAsyGet(String uid, String areaid, String page, AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);

        this.uid = uid;

        this.areaid = areaid;

        this.page = page;

        ASY_TYPE = AsyType.MAX_STALE;

    }

    public Info parser(JSONObject object){

        if(object.optString("message").equals("1")){

            Info info = new Info();

            info.nextpage = object.optString("nextpage");

            info.ispage = object.optString("ispage");

            JSONArray array1 = object.optJSONArray("bannerlist");

            if(array1 != null){

                for(int i = 0 ; i < array1.length() ; i++){

                    JSONObject o = array1.optJSONObject(i);

                    Banner banner = new Banner();

                    banner.picurl = Conn.SERVICE + o.optString("picurl");

                    banner.type = o.optString("type");

                    banner.info = o.optString("info");

                    info.banners.add(banner);

                }

            }

            JSONArray array2 = object.optJSONArray("flaglist");

            if(array2 != null){

                for(int i = 0 ; i < array2.length() ; i++){

                    JSONObject o = array2.optJSONObject(i);

                    Flag flag = new Flag();

                    flag.picurl = Conn.SERVICE + o.optString("picurl");

                    flag.type = o.optString("type");

                    flag.info = o.optString("info");

                    flag.name = o.optString("name");

                    info.flags.add(flag);

                }

            }

            JSONArray array3 = object.optJSONArray("guanggaolist");

            if(array3 != null){

                for(int i = 0 ; i < array3.length() ; i++){

                    JSONObject o = array3.optJSONObject(i);

                    Advert advert = new Advert();

                    advert.title = o.optString("title");

                    advert.picurl = Conn.SERVICE + o.optString("picurl");

                    advert.type = o.optString("type");

                    advert.info = o.optString("info");

                    info.adverts.add(advert);

                }

            }

            JSONArray array4 = object.optJSONArray("enjoylist");

            if(array4 != null){

                GoodDouble goodDouble = new GoodDouble();

                for(int i = 0 ; i < array4.length() ; i++){

                    JSONObject o = array4.optJSONObject(i);

                    Good good = new Good();

                    good.picurl = Conn.SERVICE + o.optString("picurl");

                    good.pid = o.optString("pid");

                    good.title = o.optString("title");

                    good.price = o.optString("price");

                    good.unit = o.optString("unit");

                    good.number = o.optString("salenumber");

                    if(goodDouble.goodOne == null){

                        goodDouble.goodOne = good;

                        if(i == array4.length() - 1){

                            info.enjoyDoubles.add(goodDouble);

                        }

                    }else{

                        goodDouble.goodTwo = good;

                        info.enjoyDoubles.add(goodDouble);

                        goodDouble = new GoodDouble();

                    }

                }

            }

            return info;

        }

        return null;

    }

    public static class Info implements Serializable{

        public String nextpage, ispage;

        public List<Banner> banners = new ArrayList<>();

        public List<Flag> flags = new ArrayList<>();

        public List<Advert> adverts = new ArrayList<>();

        public List<GoodDouble> enjoyDoubles = new ArrayList<>();

    }

    public static class Banner implements Serializable{

        public String picurl, type, info;

    }

    public static class Flag implements Serializable{

        public String picurl, type, info, name;

    }

    public static class Advert implements Serializable{

        public String title, picurl, type, info;

    }

    public static class GoodDouble implements Serializable{

        public Good goodOne, goodTwo;

    }

}
